import { _decorator, Component, Label, warn, log, Color, color, v4, assetManager, Material } from 'cc';
const { ccclass, property } = _decorator;

@ccclass('labeltextstyle1')
export class labeltextstyle1 extends Component {

    _label: Label = null;

    @property
    _startColor: Color = color(255, 255, 255, 255);
    @property(Color)
    get startColor(): Color { return this._startColor; }
    set startColor(v: Color) {
        this._startColor = v;
        this.refreshMaterial();
    }

    @property
    _endColor: Color = color(255, 255, 255, 255);
    @property(Color)
    get endColor(): Color { return this._endColor; }
    set endColor(v: Color) {
        this._endColor = v;
        this.refreshMaterial();
    }

    colorToV4(color: Color) {
        return v4(color.r / 255, color.g / 255, color.b / 255, color.a / 255);
    }

    refreshMaterial() {
        if (this._label == null) this._label = this.node.getComponent(Label);
        if (this._label == null) {
            warn('need Label Component');
            return;
        }

        let material = this._label.getMaterialInstance(0);
        if (material) {
            material.setProperty('startColor', this.colorToV4(this.startColor));
            material.setProperty('endColor', this.colorToV4(this.endColor));
            this._label.setMaterialInstance(material, 0);
        }

    }
}

